package com.genius.adminmanager.group.service.impl;


import java.sql.SQLException;
import java.text.DecimalFormat;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.group.entity.GroupEntity;
import com.genius.adminmanager.group.service.GroupServiceAdapter;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.workflow.config.DefaultConfiguration;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBUtils;

@Service("groupInsertService")
public class GroupInsertServiceImpl extends GroupServiceAdapter{

	@Override
	public JSONObject insert(JSONObject json, JSONObject response) {
		System.out.println(">>>"+json);
		String groupId = json.getString(GroupEntity.groupId);
		try {
			if(groupId.equals("-1")){
				//说明是根目录下,直接插入
				insertRoot(json);
			}else{
				insertSub(json);
			}
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, "添加组织机构成功");
		} catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Add_Data_Error);
		}
		
		return response;
	}
	//根目录下新增菜单
	private void insertRoot(JSONObject json) throws ConnectionException, DBClosedException, SQLException{
		String status = json.containsKey(GroupEntity.status)?"1":"0";
		json.put(GroupEntity.status, status);
		json.put(GroupEntity.state, 1);
		String table = "t_group";
		
//		//查询目录的path路径
//		String pathSql = "select "+GroupEntity.path+" FROM "+table+
//				" where "+GroupEntity.groupId+
//				" = (select max("+GroupEntity.groupId+") from "+table+" where "+GroupEntity.state+"=1)";
//		JSONObject pathJson = DBUtils.select(pathSql);
//		String fullPath = pathJson == null?"001":parsePathMax(pathJson.getString(GroupEntity.path));
		json.put(GroupEntity.path, getGroupPath(json.getString(GroupEntity.groupId)));
		json.put(GroupEntity.pId, "-1");
		
		String insertField = GroupEntity.groupId+","+GroupEntity.name+","+GroupEntity.state+","+
				GroupEntity.path+","+GroupEntity.pId+","+GroupEntity.status;
//		String insertSql = DBTools.getInsertSql(insertField, table);
//		String [] insertParms = DBTools.getSelectParms(insertField, json);
		DBTools.insert(insertField, table, json);
	}
	private String parsePathMax(String path){
		DecimalFormat df = new DecimalFormat(DefaultConfiguration.TreePathTpl);
		String oldPath = path.substring(path.length()-DefaultConfiguration.TreePathTpl.length());
		int oldPathInt = Integer.parseInt(oldPath);
		int newPathInt = oldPathInt+1;
		String newPath = df.format(newPathInt);
		String fullPath = path.substring(0, path.length()-DefaultConfiguration.TreePathTpl.length())+newPath;
		return fullPath;
		
	}
	private void insertSub(JSONObject json) throws ConnectionException, DBClosedException, SQLException{
		String status = json.containsKey(GroupEntity.status)?"1":"0";
		json.put(GroupEntity.status, status);

		String groupId = json.getString(GroupEntity.groupId);
		String table = "t_group";
		String lastSql = "select "+GroupEntity.state+","+GroupEntity.path+" from t_group where "+GroupEntity.groupId+"=?";
		//查找上层的级别
		JSONObject lastJson = DBUtils.select(lastSql, new String[]{groupId});
		//现在的级别就是在上层的基础上+1
		String nowState = lastJson.getInteger(GroupEntity.state)+1+"";
//		//查询上层的path路径
//		String pathSql = "select "+GroupEntity.path+" FROM "+table+
//				" where "+GroupEntity.groupId+
//				" = (select max("+GroupEntity.groupId+") from "+table+" where "+GroupEntity.pId+"='"+groupId+"')";
//		JSONObject pathJson = DBUtils.select(pathSql);
//		String fullPath = null;
//		if(pathJson != null){
//			fullPath = parsePathMax(pathJson.getString(GroupEntity.path));
//		}else{
//			//如果为空，说明当前底下没有子信息了，路径就为1
//			fullPath = lastJson.getString(GroupEntity.path)+"/001";
//		}
		json.put(GroupEntity.path, getGroupPath(groupId));
		json.put(GroupEntity.pId, groupId);
		json.put(GroupEntity.state, nowState);
		
		String insertField = GroupEntity.groupId+","+GroupEntity.name+","+GroupEntity.state+","+
				GroupEntity.path+","+GroupEntity.pId+","+GroupEntity.status+",shopid";
		DBTools.insert(insertField, table, json);
	}
	//获取path
	private String getGroupPath(String pId) throws ConnectionException, DBClosedException, SQLException{
		StringBuilder pathSb = new StringBuilder();
		//根目录下直接取groupId
		if(!"-1".equals(pId)){
			String pathSql = "select path,groupId from t_group where groupId = '"+pId+"'";
			JSONObject pathJson = DBUtils.select(pathSql);
			pathSb.append(pathJson.getString("path")+"/");
		}
		return pathSb.toString();
	}
	
}
